package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.entity.Banner;
import com.itheima.result.Result;
import com.itheima.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 轮播图管理
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return PageInfo<Movies>
     */
    @GetMapping("list")
    public Result findByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            String title) {
        //调用service完成分页查询
        PageInfo<Banner> list = bannerService.findByPage(pageNum, pageSize, title);
        return Result.success(list);
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return movies
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Banner banner = bannerService.findById(id);
        return Result.success(banner);
    }

    /**
     * 新增或修改
     *
     * @param
     * @return 无
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Banner banner) {
        if (banner.getId() == null) {
            // 新增
            bannerService.save(banner);
        } else {
            // 修改
            bannerService.update(banner);
        }
        return Result.success();
    }

    /**
     * 根据id删除
     *
     * @param ids 主键或主键数组
     * @return 无
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable Integer... ids) {
        if (ids.length == 1) {
            // 单个ID
            bannerService.deleteById(ids[0]);
        } else {
            // 多个ID
            bannerService.deleteBatch(ids);
        }
        return Result.success();
    }

}